﻿Imports System.Data

Public Class CheckBoxList
    Public dtParentTable As DataTable = Nothing
    Public dtChildTable As DataTable = Nothing
    Public dtRelationDataSource As DataTable = Nothing

    Public SelectedParentID As Integer = Nothing
    Public ParentIDMember As String = Nothing
    Public ChildIDMember As String = Nothing
    Public DisplayMember As String = Nothing
    Public ValueMember As String = Nothing

    Function BindData()
        If IsNothing(dtParentTable) Then _
            Throw New ArgumentException("dtParentTable: must be set ")
        If IsNothing(dtChildTable) Then _
                Throw New ArgumentException("dtChildTable: must be set ")
        If IsNothing(dtRelationDataSource) Then _
                Throw New ArgumentException("dtRelationDataSource: must be set ")
        If IsNothing(ParentIDMember) Then _
                Throw New ArgumentException("ParentIDMember: must be set ")
        If IsNothing(ChildIDMember) Then _
                Throw New ArgumentException("ChildIDMember: must be set ")
        If IsNothing(DisplayMember) Then _
                Throw New ArgumentException("DisplayMember: must be set ")
        If IsNothing(ValueMember) Then _
                Throw New ArgumentException("ValueMember: must be set ")
        Try
            Dim iTop As Integer = 0
            pnlSlider.Controls.Clear()

            For Each dr As DataRow In dtChildTable.Rows
                Dim chkItem As New CheckBox
                chkItem.Parent = pnlSlider
                chkItem.Text = dr.Item(DisplayMember)
                chkItem.Tag = dr.Item(ValueMember)
                chkItem.Top = iTop
                chkItem.Visible = True
                iTop += 20
                AddHandler chkItem.CheckStateChanged, AddressOf chkItem_CheckStateChanged
                If pnlSlider.Height <= iTop + 30 Then
                    pnlSlider.Height = iTop + 30
                End If
                CheckCheck(chkItem)
            Next
            If pnlSlider.Height > pnlMain.Height Then
                VScrollBar1.Maximum = pnlSlider.Height / 2
                VScrollBar1.Enabled = True
            Else
                VScrollBar1.Enabled = False
            End If
            Return True

        Catch ex As Exception
            Throw ex
        End Try

    End Function

    Sub CheckCheck(ByRef ckBox As CheckBox)
        dtRelationDataSource.DefaultView.RowFilter = ParentIDMember & " =" & SelectedParentID & _
                                                     " AND " & ChildIDMember & " =" & ckBox.Tag
        If dtRelationDataSource.DefaultView.Count > 0 Then
            ckBox.Checked = True
        Else
            ckBox.Checked = False
        End If

    End Sub

    Private Sub chkItem_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim ckBox As CheckBox = CType(sender, CheckBox)
        
        dtRelationDataSource.DefaultView.RowFilter = ParentIDMember & " =" & SelectedParentID & _
                                                     " AND " & ChildIDMember & " =" & ckBox.Tag
        If ckBox.Checked = False And dtRelationDataSource.DefaultView.Count > 0 Then
            For iRow As Integer = 0 To dtRelationDataSource.DefaultView.Count - 1
                dtRelationDataSource.DefaultView(iRow).Delete()
            Next
        ElseIf dtRelationDataSource.DefaultView.Count <= 0 Then
            Dim dr As DataRow = dtRelationDataSource.NewRow
            dr.Item(ParentIDMember) = SelectedParentID
            dr.Item(ChildIDMember) = ckBox.Tag
            dtRelationDataSource.Rows.Add(dr)
        End If
        
    End Sub

    Private Sub VScrollBar1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VScrollBar1.ValueChanged
        pnlSlider.Top = VScrollBar1.Value * -1
    End Sub
End Class
